module.exports =app=>{
    const express= require("express")
    const router= express.Router()
    const CategoryModel = require('../../models/Category')
    router.post('/categories',async(req,res)=>{
        const result = await CategoryModel.create(req.body)
        res.send({code:200,data:result,msg:'创建成功!'})
    })
    router.put('/categories/:id',async(req,res)=>{
        const result = await CategoryModel.findByIdAndUpdate(req.params.id, req.body)
        res.send({code:200,data:result,msg:'创建成功!'})
    })
    router.delete('/categories/:id',async(req,res)=>{
        const result = await CategoryModel.findByIdAndRemove(req.params.id)
        res.send({success:true})
    })
    router.get('/categories',async(req,res)=>{
        const data = await CategoryModel.find({}).populate('parent').limit(10)
        res.send(data)
    })
    router.get('/categories/:id',async(req,res)=>{
        const model = await CategoryModel.findById(req.params.id)
        res.send(model)
    })
    app.use('/admin/api',router)
}